﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FTchina;

public partial class smstask_autoResend : System.Web.UI.Page
{

    private object OpLock = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        lock (OpLock)
        {
            AutoRestSending(100);
            AutoReSend(100);
            AutoReSend2(20);
        }
    }


    /// <summary>
    /// 自动重置
    /// </summary>
    protected void AutoReSend(int num)
    {
        /***
      * 处理拉取成功，未发送的，也就是有lockguid，但是仍处于未发送状态的
      * 找到后，比对发送队列，有则忽略，发送队列没有的话则重置为未拉取，删除lockguid
      * **/
        string sql = "select top " + num + " id,smsguid,lockguid from sms_log where (taskstate=2 or taskstate=0) and state =2 and lockguid<>'' order by id asc";
 
        using (DataTable dt = shujuku.GetDataTable(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (CheckSmsSendinglockid(dr["smsguid"].ToString())) continue;
                ResetSmsState(dr["smsguid"].ToString());
                LogNet.LogBLL.info("自动重新队列" + dr["smsguid"].ToString());

            }

        }
    }
    /// <summary>
    /// 自动重置
    /// </summary>
    protected void AutoReSend2(int num)
    {
        /***
      * 处理拉取成功，发送失败的，也就是有lockguid，但是仍处于未发送状态的
      * 找到后，比对发送队列，有则忽略，发送队列没有的话则重置为未拉取，删除lockguid
      * **/
        string sql = "select top "+num+"  id,smsguid,lockguid from sms_log where (taskstate=1) and sendcount=1 and state =0 and lockguid<>'' and smsguid not in (select smsguid from sms_sended) order by id asc";

        using (DataTable dt = shujuku.GetDataTable(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (CheckSmsSendinglockid(dr["smsguid"].ToString())) continue;
                ResetSmsState(dr["smsguid"].ToString());
                LogNet.LogBLL.info("自动重新队列2" + dr["smsguid"].ToString());

            }

        }
    }
    /// <summary>
    /// 查看是否在发送队列
    /// </summary>
    /// <param name="smsguid"></param>
    /// <returns></returns>
    private Boolean CheckSmsSendinglockid(string smsguid)
    {
        string sql = "select top 1 smsguid from sms_sending where smsguid='" + smsguid + "'";
        return shujuku.testread(sql);
    }
    private Boolean ResetSmsState(string smsguid)
    {
        string sql = "update sms_log set taskstate=2 , lockguid='',sendcount=0 where smsguid='" + smsguid + "'";
        return shujuku.ExecuteSql(sql);
    }



    /// <summary>
    /// 重新处理队列中的短信
    /// </summary>
    /// <param name="num"></param>
    private void AutoRestSending(int num)
    {
        /**
         * 先查询id最小的，即最先进入通道的，
         * 看smsguid对应的sms_log是否为已发送状态，已发送，删除sms_sending,未发送或者发送失败则删除lockguid，重新触发发送条件
         * **/

        string sql = "SELECT top " + num + " ss.smsguid,sl.state  FROM [cms].[dbo].[Sms_sending] as ss ,sms_log as sl where ss.smsguid=sl.smsguid  ";
        using (DataTable dt = shujuku.GetDataTable(sql))
        {
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["state"].ToString() == "1")
                {//发送成功了则从sms_sending 删除
                    DeleteFromSmsSending(dr["smsguid"].ToString());
                    LogNet.LogBLL.info("自动删除队列" + dr["smsguid"].ToString());

                }
                else
                {//重新出发发送条件

                    ResetSmsSending(dr["smsguid"].ToString());
                    LogNet.LogBLL.info("自动重新发送" + dr["smsguid"].ToString());
                }
            }
        }

    }
    /// <summary>
    /// 将数据从发送队列删除
    /// </summary>
    /// <param name="smsguid"></param>
    /// <returns></returns>
    private Boolean DeleteFromSmsSending(string smsguid)
    {
        string sql = "delete from sms_sending where smsguid='" + smsguid + "'";
        return shujuku.ExecuteSql(sql);
    }
    /// <summary>
    /// 重新出发发送条件
    /// </summary>
    /// <param name="smsguid"></param>
    /// <returns></returns>
    private Boolean ResetSmsSending(string smsguid)
    {
        string sql = "update  sms_sending set lockguid='' where smsguid='" + smsguid + "'";
        return shujuku.ExecuteSql(sql);
    }
}